<template>
  <div class="home-container">
    <div class="carousel-container">
      <el-carousel trigger="click" height="200px">
        <el-carousel-item v-for="item in 4" :key="item">
          <h3 class="small">{{ item }}</h3>
        </el-carousel-item>
      </el-carousel>
    </div>

    <div class="hot-container">
      <h2 class="title">热门推荐</h2>
      <div class="content">
        <ArticleIntroduceCard v-for="article in hotArticles" :key="article.id" :article="article"/>
      </div>
    </div>

    <div class="new-container">
      <h2 class="title">最新博文</h2>
      <div class="content">
        <ArticleIntroduceCard  v-for="article in newArticles" :key="article.id" :article="article" />
      </div>
    </div>
  </div>
</template>

<script>
import { baseUrl } from "../api";
import ArticleIntroduceCard from '../components/ArticleIntroduceCard'
export default {
  name: "Home",
  components: {
    ArticleIntroduceCard
  },
  data() {
    return {
      hotArticles: [],
      newArticles: []
    }
  },
  methods: {
    getHotArticleList() {
      this.$http.get('/home/getHotArticleList').then(response=>{
        let result= response.data;
        if(result.code===200) {
          this.hotArticles = JSON.parse(result.data);
          for(let i=0; i<this.hotArticles.length; i++) {
            this.hotArticles[i].coverImg = baseUrl + this.hotArticles[i].coverImg;
          }
        }
      })
    },
    getNewArticleList(currPage, pageSize) {
      const url = '/home/getNewArticleList';
      this.$http.get(url, {
        data: {
          currPage,
          pageSize
        }
      }).then(response=>{
        let result= response.data;
        if(result.code===200) {
          this.newArticles = JSON.parse(result.data);
          for(let i=0; i<this.newArticles.length; i++) {
            this.newArticles[i].coverImg = baseUrl + this.newArticles[i].coverImg;
          }
        }
      })
    },
  },
  mounted() {
    this.getHotArticleList();
    this.getNewArticleList();
  }

}
</script>

<style scoped lang="less">
.home-container {
  width: 100%;
  .carousel-container {
    width: 100%;
    height: 200px;
  }

  .hot-container, .new-container {
    margin-bottom: 30px;
    padding: 20px;
    text-align: left;
    .title {
      font-size: 28px;
      line-height: 32px;
      height: 32px;
      color: #333;
      margin: 0 0 8px;
    }

    .content {
      width: 100%;
      display: flex;
      flex-direction: row;
      align-items: center;
      //justify-content: space-between;
      flex-wrap: wrap;
    }
  }
}
.el-carousel__item h3 {
  color: #333;
  font-size: 14px;
  opacity: 0.75;
  line-height: 150px;
  margin: 0;
}
.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n+1) {
  background-color: #d3dce6;
}
</style>
